
# Libraries ---------------------------------------------------------------

library(tidyverse)
library(haven)
library(rgdal)

governadores <- read_dta("../data/data_final.dta") %>%
  mutate(uf_cod = as.numeric(as.character(uf_cod)))


# Base map (Brazil) -------------------------------------------------------

mapa <- readOGR(dsn = '../shapefile', layer = 'UFEBRASIL') # read shapefile

mapa@data$ID <- 0:(nrow(mapa@data) - 1) # create a id for data

brasil_uf <- fortify(mapa) %>% # convert coords. to data frame
  mutate(ID = as.numeric(id)) %>% # create id for coords. table
  left_join(mapa@data, by = 'ID') %>% # merge
  mutate(uf_cod = as.numeric(as.character(CD_GEOCODU))) %>% # create uf id
  left_join(governadores, by = 'uf_cod') # merge with dataset

# Maps --------------------------------------------------------------------

g <- brasil_uf %>%
  filter(year >= 1994 & elecyear == 1) %>%
  ggplot(aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = unempch_rel)) +
  coord_map() + facet_wrap( ~ year) +
  scale_fill_continuous(low = "#d9d9d9", high = '#000000', name = 'Unemployment change') +
  labs(x = '', y = '') +
  theme_bw()


ggsave('../figures/figure1.pdf', width = 12, height = 10, plot = g)
ggsave('../figures/figure1.png', width = 12, height = 10, plot = g)
